<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>倒计时效果</title>
<script type="text/javascript">
	var DifferenceHour = -1;
	var DifferenceMinute = -1;
	var DifferenceSecond = -1;
	var dSecs = -1;
	var Tday = new Date("2011, 10, 1");
	var daysms = 24 * 60 * 60 * 1000;
	var hoursms = 60 * 60 * 1000;
	var minutesms = 60 * 1000;
	var microsecond = 1000;
	
	function clock() {
		//分别获取当前的小时,分钟和秒
		var time = new Date();
		var hour = time.getHours();
		var minute = time.getMinutes();
		var second = time.getSeconds();
		
		//按照格式显示当前时间
		var timevalue = "" + ((hour>12) ? hour-12 : hour);
		timevalue += ((minute<10) ? ":0" : ":") + minute;
		timevalue += ((second<10) ? ":0" : ":") + second;
		timevalue += (hour>12) ? " PM" : " AM"; 
		
		var convertHour = DifferenceHour;
		var convertMinute = DifferenceMinute;
		var convertSecond = DifferenceSecond;
		//返回终点时间和当前时间的毫秒差,然后计算时分秒数
		var Diffms = Tday.getTime() - time.getTime();
		DifferenceHour = Math.floor(Diffms / daysms);
		Diffms -= DifferenceHour * daysms;
		DifferenceMinute = Math.floor(Diffms / hoursms);
		Diffms -= DifferenceMinute * hoursms;
		DifferenceSecond = Math.floor(Diffms / minutesms);
		Diffms -= DifferenceSecond * minutesms;
		dSecs = Math.floor(Diffms / microsecond);
		
		//给form赋值
		if(convertHour != DifferenceHour)
			document.formnow.dd.value = DifferenceHour;
		if(convertMinute != DifferenceMinute)
			document.formnow.hh.value = DifferenceMinute;
		if(convertSecond != DifferenceSecond)
			document.formnow.mm.value = DifferenceSecond;
		document.formnow.ss.value = dSecs;
		//每秒更新一次
		setTimeout("clock()", 1000);

	}
</script>
</head>
<body onload="clock(); return true">
	<h1>倒计时效果</h1><hr>
	<form name="formnow" id="formnow">
		<p>距离还有
		<input type="text" name="dd" size="2">天
		<input type="text" name="hh" size="2">小时
		<input type="text" name="mm" size="2">分
		<input type="text" name="ss" size="2">秒	
		</p>
	</form>
</body>
</html>